<?php

include_once '../tools/common.php';
include_once '../tools/DbTools.php';


$is_login = getLoginStatus();
if(!$is_login){
    echo '尚未登入，<a href="../user/loginUi.php">请登入</a>';
    die;
}else{
    session_start();
    $userInfo = $_SESSION[$_COOKIE['Token']];
    $realName = $userInfo['realName'];
    $cardId = $userInfo['CardId'];

    DbTools::InitDb();
    $sql = "select * from bankcard where AccountId = 
    (select AccountId from accountinfo where RealName = '$realName' and CardId != $cardId) ";
    $result = DbTools::select($sql);
    //关闭连接
    DbTools::close();

    if (empty($result)){
        echo '<p><a href="../user/userInfo.php">回到用户中心</a></p><br>';
        echo '您账户下只存在一张卡！！';
        die;
    }
    echo '<p><a href="../user/userInfo.php">回到用户中心</a></p><br>';

    for ($i=0;$i<count($result);$i++) {


        DbTools::InitDb();
        echo '卡ID:' . $result[$i]['CardId'];
        echo '<br>';
        echo '<br>';
        $otherCardId = $result[$i]['CardId'];

        //将其它卡余额转给当前卡
        $result_transfer = transferMoney($otherCardId, $cardId, $result[$i]['CardMoney']);

        //修改其它卡状态
        DbTools::InitDb();
        $sql_delete = "update bankcard set CardState = -1 where CardId= '$otherCardId'";
        $result_state = DbTools::noSelect($sql_delete);

        if ($result_transfer['status'] && $result_state['status']) {
            echo '操作成功';
            echo '<br>';
            echo '<br>';
        } else {
            echo '操作失败: ' . $result_transfer['message'].'  '. $result_state['message'];
            echo '<br>';
            echo '<br>';
        }
    }

}
